
import rospy
from lab_automation.srv import TapingService, TapingServiceResponse
from lab_automation.sealing import TapingMachineController  # 假设你已保存控制器类为该模块

class TapingServiceNode:
    def __init__(self):
        rospy.init_node('taping_service_node', anonymous=True)
        self.controller = TapingMachineController()
        self.service = rospy.Service('taping_service', TapingService, self.handle_taping_request)
        rospy.loginfo("Taping service is ready.")

    def handle_taping_request(self, req):
        rospy.loginfo(f"Received request: {req.request}")
        if req.request != "seal":
            return TapingServiceResponse(response="Invalid request. Expected 'tiemo'.")

        try:
            # 设置封膜时间
            self.controller.set_sealing_time(8)
            
            # 设置封膜温度
            self.controller.set_sealing_temperature(180)
            
            # 检查是否准备好
            if not self.controller.is_ready_for_sealing(target_temperature=180):
                return TapingServiceResponse(response="Device not ready for sealing.")
            
            # 开始封膜
            self.controller.start_sealing()
            
            # 等待封膜完成
            self.controller.is_sealing_complete()
            # while not self.controller.is_sealing_complete():
                # rospy.sleep(0.1)
            
            return TapingServiceResponse(response="Sealing process completed successfully.")
        
        except Exception as e:
            rospy.logerr(f"Taping operation failed: {e}")
            return TapingServiceResponse(response=f"Taping operation failed: {str(e)}")

    def run(self):
        rospy.spin()

if __name__ == "__main__":
    node = TapingServiceNode()
    node.run()